<?php
	session_start();
	
	require_once('../../lib/'.'BaseDir.inc.htm');
	require_once (EIU_LIB.'View.class.htm');
	require_once('../../lib/'.'DBCommon.class.htm');
	
	//Load về một phần danh sách (như tìm kiếm)
	if(isset($_POST['fullLoad']) && !empty($_POST['fullLoad']) && $_POST['fullLoad'] == 1){
		
		$q = "
			SELECT 
				CusID AS id,ComName AS name,ShortName,ComAddress,ComTel,ComFax,ParentID as pId,BankName,BankCN,BankAcc,isDeleted,CreateDate,LastUpdate,ModifiedBy
			FROM `cus_list_tbl`
			WHERE `isDeleted`=0 
			ORDER BY `ComName` 
		";
		//echo $q;
		
		$oDB = new DBCommon();
		$conn =  $oDB->stl_openConn();
		
		$rs = $oDB->stl_db_select($q, $conn);
		
		/*
			var zNodes =[
				{ id:9, pId:0, name:"Võ Tấn Lực", open:true},
				{ id:10, pId:0, name:"Võ Hoàng Nguyên", open:true},
				{ id:11, pId:10, name:"Mai"},
				{ id:12, pId:9, name:"Phạm Thị Thiện Trang"},
				{ id:13, pId:12, name:"Võ Thị Ánh Tuyết"},
				{ id:14, pId:9, name:"Võ Văn Trọng"}			
			];
		*/
		
		$i = 0;
		if(is_array($rs)){		
			echo json_encode($rs);
		}else{
			echo 'NG';
		}
		
		$oDB->stl_closeConn($conn);
	}
	else//Load theo tìm kiếm
	{
		if(isset($_POST['page']) && !empty($_POST['page'])){
			$startLimit = ($_POST['page']-1)*10;
			$endLimit = 10;
		}

		$condition = "`ComName` LIKE '%".$_POST['compname']."%' AND `ComTel` LIKE'%".$_POST['comptel']."%'";
		
		$arr = "";

		$count = "SELECT COUNT(CusID) as count FROM `cus_list_tbl` WHERE `isDeleted`=0 AND ".$condition;
		//$q = "SELECT CusID,ComName,ShortName,ComAddress,ComTel,ComFax,ParentID,BankName,BankCN,BankAcc,isDeleted,CreateDate,LastUpdate,ModifiedBy FROM cus_list_tbl WHERE `isDeleted`=0 AND ".$condition." ORDER BY `CusID` LIMIT ".$startLimit.",".$endLimit;
		$q = "
			SELECT 
				CusID,ComName,ShortName,ComAddress,ComTel,ComFax,ParentID,BankName,BankCN,BankAcc,isDeleted,CreateDate,LastUpdate,ModifiedBy,ComName2,ComTel2
			FROM `cus_list_tbl` LEFT JOIN 
				(SELECT CusID AS ParentID2,ComName AS ComName2,ComTel AS ComTel2 FROM cus_list_tbl WHERE isDeleted = 0) AS B 
				ON ParentID =  ParentID2 
			WHERE `isDeleted`=0 AND ".$condition." 
			ORDER BY `ComName` 
			LIMIT ".$startLimit.",".$endLimit;
		
		//print_r($q);
		
		$oDB = new DBCommon();
		$conn =  $oDB->stl_openConn();
		
		$rs = $oDB->stl_db_select($q, $conn);
		$rs_count = $oDB->stl_db_select($count, $conn);
		if(!empty($rs_count[0]->count)){
			$arr["Count"] = $rs_count[0]->count;
		}
		
		$i = 0;
		if(is_array($rs)){
			foreach($rs as $key=>$value){
				$data1['cusId'] = $rs[$key]->CusID;
				$data1['companyName'] = $rs[$key]->ComName;
				$data1['shortname'] = $rs[$key]->ShortName;
				$data1['address'] = $rs[$key]->ComAddress;
				$data1['companyTel'] = $rs[$key]->ComTel;
				$data1['fax'] = $rs[$key]->ComFax;
				$data1['bankname'] = $rs[$key]->BankName;
				$data1['bankcn'] = $rs[$key]->BankCN;
				$data1['bankacc'] = $rs[$key]->BankAcc;
				$data1['parentid'] = $rs[$key]->ParentID;			
				$data1['parentname'] = $rs[$key]->ComName2;	
				$data1['parenttel'] = $rs[$key]->ComTel2;	
				$arr["Result"][$i] = $data1;
				$i++;
			}
			echo json_encode($arr);
		}else{
			echo 'NG';
		}
		
		$oDB->stl_closeConn($conn);
	}

?>
